package org.niux.AutoLoginQQ.timer;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.TimerTask;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swt.widgets.Display;
import org.niux.AutoLoginQQ.VO.QQNumVO;
import org.niux.AutoLoginQQ.common.Constant;
import org.niux.AutoLoginQQ.operate.LoginQQ;
import org.niux.AutoLoginQQ.ui.Main;

/**
 * 
 * Copyright (c) 2009,niux<br>
 * All rights reserved.<br>
 * 
 * 文件名称：ScanTaskTimer.java<br>
 * 摘    要：监测登录队列的轮循器<br>
 * <br>
 * 当前版本：<br>
 * 作    者：mingxian Email: <a href="mailto:liangmingxian@gmail.com">liangmingxian@gmail.com</a><br>
 * 完成日期：@since 2012-9-12<br>
 * <br>
 * 取代版本：<br>
 * 原作者  ：<br>
 * 完成日期：@since Oct 22, 2009<br>
 */
public class SendPollTimer extends TimerTask {
	
	private Log log = LogFactory.getLog(this.getClass());
	
	private Main gui;
	
	public SendPollTimer(Main gui) {
		super();
		this.gui = gui;
	}

	@Override
	public void run() {
		log.debug("==========心跳包轮循器开始监测=============");
		log.debug("已登录队列中有"+ Constant.ALREADY_LOGIN_QUEEN.size()+"个QQ");
		
		List<QQNumVO> alreadyLoginQueen = new ArrayList<QQNumVO>();
		
		alreadyLoginQueen.addAll(Constant.ALREADY_LOGIN_QUEEN);
		
		Iterator<QQNumVO> i = alreadyLoginQueen.iterator();
		
		while(i.hasNext()){
			QQNumVO qq = i.next();
			
			long totalLoginTime = System.currentTimeMillis() - qq.getLoginTime() + 
					(Constant.QQ_LIST.get(qq.getQqNum()).getAlreadyLogin() == null ? 0 : Constant.QQ_LIST.get(qq.getQqNum()).getAlreadyLogin());
			
//			long currentLoginTime = System.currentTimeMillis() - qq.getLoginTime();
			
			log.debug(qq.getQqNum() + "当天总共登录时间是:" + totalLoginTime);
			
//			Constant.QQ_LIST.get(qq.getQqNum()).setAlreadyLogin(totalLoginTime);
			
			updateLoginTime(qq.getIndex(),format(totalLoginTime),qq.getQqNum(),totalLoginTime);
			
			LoginQQ loginQQ = new LoginQQ(gui);
			
			//判断是否登录时间超过限制
 			log.debug("登录时间限制是:" + Long.parseLong(Constant.LOGIN_TIME) * 60 * 1000);
 			log.debug("当前登录时间是:" + totalLoginTime);
 			if(totalLoginTime > Long.parseLong(Constant.LOGIN_TIME) * 60 * 1000){
 				log.debug(qq.getQqNum() + "登录限时已到，退出登录");
 				loginQQ.addLogoutQueen(qq.getQqNum(), "登录限时已到");
 				continue;
 			}
			
			if(qq.getSentPollResponse() == null){
				log.debug(qq.getQqNum()+"发送的心跳包请求尚未返回");
				continue;
			}
			
			log.debug(qq.getQqNum()+"发送心跳包");
			qq.setSentPollResponse(null);
			
			loginQQ.sentPoll(qq.getHttpClient(), qq.getPsessionid(), qq);
		}
	}
	
	/**
	 * 
	 * 方法概述：更新挂机时间
	 * 描述：更新挂机时间
	 * @param index
	 * @param loginTime
	 * 返回类型：void
	 * 修改人：梁铭显
	 * 修改时间：2012-9-11
	 */
	public void updateLoginTime(final int index, final String loginTime, final String num, final long time) {
		Display.getDefault().asyncExec(new Runnable() {
			public void run() {
				gui.updateLoginTime(index, loginTime,num,time);
			}
		});
	}
	
	/**
	 * 
	 * 方法概述：将毫秒数换算成x时x分x秒
	 * 描述：将毫秒数换算成x时x分x秒
	 * @param ms
	 * @return
	 * 返回类型：String
	 * 修改人：梁铭显
	 * 修改时间：2012-9-11
	 */
	public String format(long ms) {// 将毫秒数换算成x时x分x秒
		int ss = 1000;
		int mi = ss * 60;
		int hh = mi * 60;
		int dd = hh * 24;

		long day = ms / dd;
		long hour = (ms - day * dd) / hh;
		long minute = (ms - day * dd - hour * hh) / mi;
		long second = (ms - day * dd - hour * hh - minute * mi) / ss;

		String strHour = hour < 10 ? "0" + hour : "" + hour;
		String strMinute = minute < 10 ? "0" + minute : "" + minute;
		String strSecond = second < 10 ? "0" + second : "" + second;
		
		return strHour + ":" + strMinute + ":" + strSecond;
	}

}
